<!--绑定用户信息-->
<template>
	<view class="content">
		<view class="box">
			<ul>
				<!-- <li>证件类型</li> -->
				<!-- <li>
					<view @click="healthCardHref()">一键绑定注册健康卡</view>
				</li> -->
				<li>
					<text class="left">身份证号</text>
					<input v-if="flag" type="text" placeholder="请输入身份证号" v-model="data.kh0000" disabled
						maxlength="18" />
					<input v-else type="text" placeholder="请输入身份证号" @blur="cardOver" v-model="data.kh0000"
						maxlength="18" />
				</li>
				<li>
					<text class="left">年龄</text>
					<input type="text" v-model="data.brnl00" placeholder="请输入年龄" disabled maxlength="3" />
				</li>

				<li>
					<text class="left">姓名</text>
					<input type="text" v-model="data.xming0" maxlength="4" placeholder="请输入真实姓名" />
				</li>
				<li style="display: none;">
					<text class="left">卡类型</text>
					<input type="text" v-model="data.klx000" maxlength="4" value="3" />
				</li>
				<li style="display: none;">
					<text class="left">身份识别码</text>
					<input type="text" v-model="data.hzsfbh" maxlength="4" value="0" />
				</li>
				<li>
					<text class="left">联系电话</text>
					<input type="text" v-model="data.lxdh00" maxlength="11" placeholder="请输入联系电话" />
				</li>
				<li>
					<text class="left">关系:</text>
					<view style="width: 80%;float: right;text-align: right;margin-right: 10px;">
						<picker @change="relationChange" :value="relationIndex" :range="relationArray">
							<view class="uni-input">{{relationArray[relationIndex]}}
								<view class="type_icon"></view>
							</view>
						</picker>
					</view>
					<input type="text" style="display: none;" v-model="data.relation" placeholder="请输入真实姓名" />
				</li>
				<li>
					<text class="left">性别:</text>
					<view style="width: 80%;float: right;text-align: right;margin-right: 10px;">
						<picker @change="sexChange" :value="sexIndex" :range="sexArray">
							<view class="uni-input">{{sexArray[sexIndex]}}
								<view class="type_icon"></view>
							</view>
						</picker>
					</view>
					<input type="text" style="display: none;" v-model="data.xbie00" placeholder="请输入真实姓名" />
				</li>
				<li>
					<text class="left">出生年月</text>
					<picker mode="date" @change="getDate">{{data.csrq00}}</picker>
					<input type="text" style="display: none;" v-model="data.csrq00" placeholder="请输入出生年月,例19850114" />
				</li>
				<li>
					<text class="left">出生地址</text>
					<input type="text" v-model="data.csdz00" maxlength="4" placeholder="请输入出生地址" />
				</li>
				<li>
					<text class="left">民族</text>
					<input type="text" v-model="data.brmz00" placeholder="请输入民族" />
				</li>
			</ul>
		</view>
		<view class="btns">
			<button class="cancel" @click="cancel()">取消</button>
			<button class="sure" @click="sure()">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {
					kh0000: "",
					csrq00: "出生日期",
					brmz00: ""
				},
				flag: true,
				sexArray: ['请选择', '男', '女'],
				sexIndex: 0,
				relationArray: ["本人", "父母", "子女", "夫妻", "亲属", "朋友", "其他"],
				relationIndex: 0,
			}
		},
		onLoad(option) {
			if (option.kh0000) {
				this.data.kh0000 = option.kh0000;
				this.init(this.data.kh0000);
			} else {
				this.flag = false;
			}
		},
		methods: {
			init(kh0000) {
				if (kh0000.length == 18) {
					let data = {
						"kh0000": kh0000,
						"klx000": "3",
						"ywlsh0": "" + new Date().getTime(),
						"funid": "F07.02.02.01"
					}
					uni.showLoading({
						mask: true,
						title: "加载中..."
					})
					this.$postJson("/qnyesb/qnyesbproxy", data, res => {
						//判断用户不存在档案信息则进行跳转
						if (res.state == 0) {
							let user = res.data;
							user.kh0000 = user.sfzh00;
							this.sexIndex = user.xbie00;
							if (!user.csdz00) {
								user.csdz00 = user.lxdz;
							}
							this.data = res.data;
						} else {
							// uni.showToast({
							// 	title:res.message
							// })
						}
						//身份证
						var csrq00 = kh0000.substr(6, 8);
						var yyyy = csrq00.substr(0, 4);
						var MM = csrq00.substr(8 - 4, 2);
						var dd = csrq00.substr(8 - 2, 2);
						var csrq = (yyyy + "-" + MM + "-" + dd);
						this.data.csrq00 = csrq;
						var brnl00 = parseInt(new Date().getFullYear()) - parseInt(yyyy);
						this.data.brnl00 = "" + brnl00;
						uni.hideLoading();
					})

				}
			},
			healthCardHref() {
				Window.location.href = "";
			},
			cardOver(e) {
				let kh0000 = e.detail.value;
				this.init(kh0000);
			},
			sexChange(e) {
				this.sexIndex = e.detail.value;
				this.data.xbie00 = "" + e.detail.value;
			},
			relationChange(e) {
				this.relationIndex = e.detail.value;
				this.data.pagx00 = "" + e.detail.value;
			},
			getDate(e) {
				this.data.csrq00 = e.detail.value;
			},
			cancel() {
				uni.navigateBack({
					delta: 1
				})
			},
			sure() {
				if (!this.data.xming0) {
					uni.showToast({
						title: "请输入姓名~",
						icon: "none"
					})
					return;
				}
				if (!this.data.lxdh00) {
					uni.showToast({
						title: "请输入联系电话~",
						icon: "none"
					})
					return;
				}
				if (!this.data.brmz00) {
					uni.showToast({
						title: "请输入民族~",
						icon: "none"
					})
					return;
				}
				//进行保存关联就诊人操作
				let data = this.data;
				//建党
				data.funid = "F07.03.02.01";
				if (!data.klx000) {
					data.klx000 = "3";
				}
				if (!data.hzsfbh) {
					data.hzsfbh = "4";
				}
				data.ywlsh0 = "" + new Date().getTime();
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				this.$postJson("/qnyesb/qnyesbproxy", data, res1 => {
					if (res1.state == 0) {
						if (this.relationIndex == 0) {
							//如果是本人，则进行模拟登录
							let data = {
								"kh0000": this.data.kh0000,
								"klx000": "3",
								"ywlsh0": "" + new Date().getTime(),
								"funid": "F07.02.02.01"
							}
							this.$post("/qnyesb/qnyesbproxy", data, userRes => {
								if (userRes.state == 0) {
									uni.setStorageSync("user", userRes.data);
								} else {

								}
							})
						}else{
							//如果不是本人
							let loginUser = uni.getStorageSync("user");
							if(!loginUser){
								uni.showToast({
									title:"您还未登录，请勿关联本人以外的关系",
									icon:"none",
									duration:3000
								})
								return;
							}
							data = {
								"kh0000": loginUser.sfzh00,
								"xming0": loginUser.xming0,
								"pakh00": data.kh0000,
								"paxm00": data.xming0,
								"panl00": data.xbie00,
								"pagx00": data.pagx00,
								"funid": "F07.02.02.03"
							}
							uni.showLoading({
								mask: true,
								title: '加载中...'
							})
							this.$postJson("/qnyesb/qnyesbproxy", data, res => {
								//判断用户不存在档案信息则进行跳转
								if (res.state == 0) {
									if (this.sexIndex == 0) {
										uni.setStorageSync("user", res.data);
									}
									uni.navigateBack({
										delta: 1
									})
								} else {
									uni.showModal({
										title: "温馨提示",
										content: res.message + "，是否继续操作",
										success(res) {
											if (res.cancel) {
												uni.navigateBack({
													delta: 1
												})
											}
										}
									})
								}
								uni.hideLoading();
							})
						}
					} else {
						uni.showModal({
							title: "温馨提示",
							content: res1.message + "，是否继续操作",
							success(res) {
								if (res.cancel) {
									uni.navigateBack({
										delta: 1
									})
								}
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		position: absolute;
		width: 100%;
	}

	.box {
		width: 100%;
		position: relative;
		top: 10px;
	}

	.box ul {
		position: relative;
		top: 0px;
	}

	.box ul li:first-child {
		text-align: center;
		color: blue;
	}

	.box ul li {
		color: #999999;
		margin: 0 auto;
		background: #ffffff;
		height: 53px;
		width: 96%;
		border-radius: 4px;
		line-height: 53px;
		text-indent: 1em;
		margin-top: 10px;
	}

	.box ul li input,
	picker {
		float: right;
		text-align: right;
		height: 100%;
		margin-right: 10px;
		width: 70%;
		font-size: 16px;
	}

	.type_icon {
		width: 12.02px;
		height: 12.02px;
		background-image: url("../../static/registeredType/124@2x.png");
		background-size: 12px 12px;
		float: right;
		margin-top: 22px;
	}

	.btns {
		width: 90%;
		margin: 0 auto;
		position: relative;
		top: 30px;
	}

	.btns button {
		border-radius: 24px;
		float: left;
		width: 47%;

	}

	.btns button:not(:first-child) {
		margin-left: 15px;
	}
</style>
